#-*-coding:utf-8-*-
import requests
from xml.etree import ElementTree  # 用于解析xml
import time

headers = {'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'}  # 请求头
text = u"亲，经过小家园的估算，您寝室电量预计使用天数已不足3天了噢，请尽快充值！【家园工作室】"  # 要发送的短信内容
send_url = "http://www.jianzhou.sh.cn/JianzhouSMSWSServer/http/sendBatchMessage"  # 发送短信的接口
acc_url = "http://www.jianzhou.sh.cn/JianzhouSMSWSServer/http/getUserInfo"  # 查询账户信息的接口
account = "sdk_ndxgc"
password = "jianzhou"


def account_info():
    params = {
        "account": account,
        "password": password
    }
    response = requests.post(acc_url, data=params, headers=headers, timeout=10)
    tree = ElementTree.fromstring(response.text)  # 使用ElementTree解析返回的xml数据
    remainFee = tree.find("remainFee")  # 找到remainFee这个节点
    return remainFee.text  # 返回账户余额(剩余条数)


def send_sms(mobile):
    """
    智能匹配模板发送
    参数为接收者手机号（字符串），多个则使用分号隔开
    """
    rawtime = time.strftime("%Y%m%d%X", time.localtime())
    localtime = "".join(rawtime.split(":"))  # 14位当地时间
    params = {'account': account, 'password': password,
              'destmobile': mobile, "msgText": text, "sendDateTime": localtime}  # 参数
    response = requests.post(send_url, data=params,
                             headers=headers, timeout=10)
    return "finish"
